package com.SFA.OrderManagement;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.FrameWork.Common.BitmapManager;
import com.FrameWork.Entity.ImagesEntity;
import com.FrameWork.Entity.ProductEntity;
import com.FrameWork.Utils.Utils;
import com.SFA.R;
import com.SFA.ProductShow.ProductPhotoList;

public class ProductListAdapter extends BaseAdapter implements OnItemClickListener{

	protected List<ProductEntity> list = null;
	protected LayoutInflater inflater;
	protected Context context = null;
	protected boolean showPicture = true, showCheckbox = true, showDeleteButton = false;
	public  OnClickListener OnRemove= null;
	private int height,width;
	private HashMap<String,SoftReference<Bitmap>> map = new HashMap<String,SoftReference<Bitmap>>();
	
	public ProductEntity SelectedEntity; 
	
	public ProductListAdapter(List<ProductEntity> list,Context context)
	{
		this.list= list;
		this.context = context;
		inflater = LayoutInflater.from(context);
	}

	//是否显示图片
	public void ShowPicture(boolean show)
	{
		showPicture = show;
		this.notifyDataSetInvalidated();
	}

	

	//是否显示删除按钮
	public void showDeleteButton(boolean show)
	{
		showDeleteButton = show;
		this.notifyDataSetInvalidated();
	}

	//设置默认背景色
	public void showCheckbox(boolean show)
	{
		showCheckbox = show;
		this.notifyDataSetInvalidated();
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(list != null)
			return list.size();
		return 0;
	}

	@Override
	public ProductEntity getItem(int position) {
		// TODO Auto-generated method stub
		if(list != null && list.size()-1>=position)
			return list.get(position);
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHoder hoder = null;
		if (convertView == null)
		{

			hoder = new ViewHoder();
			convertView = inflater.inflate(R.layout.product_list_item, null);
			hoder.imagePhoto = (ImageView) convertView.findViewById(R.id.imagePhoto);
			hoder.chkSelected = (ImageView) convertView.findViewById(R.id.chkSelected);
			hoder.btnDelete = (ImageView) convertView.findViewById(R.id.btnDelete);
			hoder.txtPrice = (TextView) convertView.findViewById(R.id.txtPrice);
			hoder.txtDescription = (TextView) convertView.findViewById(R.id.txtDescription);
			hoder.txtRemark = (TextView) convertView.findViewById(R.id.txtRemark);
			convertView.setTag(hoder);
		}
		else
		{
			hoder = (ViewHoder) convertView.getTag();
		}
		final ProductEntity entity = (ProductEntity)getItem(position);
		hoder.ProductEntity = entity;

		if(entity.Checked)
		{
			hoder.chkSelected.setBackgroundResource(android.R.drawable.checkbox_on_background);
			convertView.setBackgroundColor(Color.LTGRAY);
		}
		else
		{
			hoder.chkSelected.setBackgroundResource(R.styleable.AppTheme_listDragShadowBackground);
			convertView.setBackgroundColor(Color.WHITE);

		}
		if(height==0)
		{
			hoder.imagePhoto.measure(0, 0);
			height = hoder.imagePhoto.getMeasuredHeight();
			width= hoder.imagePhoto.getMeasuredWidth();
		}
		
		//是否显示勾选
		if(this.showCheckbox)
			hoder.chkSelected.setVisibility(View.VISIBLE);
		else
			hoder.chkSelected.setVisibility(View.INVISIBLE);
		//是否显示照片
		if(this.showPicture)
		{
			hoder.imagePhoto.setVisibility(View.VISIBLE);
			hoder.imagePhoto.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					/*Intent intent = new Intent(context,ProducxtDetailaActivity.class);
					//intent.putExtra("id", entity.PHOTO_ID);
					intent.putExtra("DATA", entity);
					context.startActivity(intent);*/
				}
			});
			hoder.show();
		}
		else
			hoder.imagePhoto.setVisibility(View.INVISIBLE);
		if(showDeleteButton)
		{
			hoder.btnDelete.setVisibility(View.VISIBLE);
			convertView.setBackgroundColor(Color.WHITE);
			hoder.btnDelete.setOnClickListener(listener);
			hoder.btnDelete.setTag(position);
		}
		else
			hoder.btnDelete.setVisibility(View.INVISIBLE);
		
		//是否显示删除按钮
		hoder.txtDescription.setText(entity.DESCRIPTION);
		hoder.txtPrice.setText(entity.PIECE_PRICE + "元");
		hoder.txtRemark.setText(entity.SHORTDESCRIPTION);

		return convertView;
	}
	
	private OnClickListener listener = new OnClickListener()
	{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
			list.remove(Integer.parseInt(v.getTag().toString()));
			if(OnRemove != null)
				OnRemove.onClick(null);
			notifyDataSetChanged();
		}
		
	};



	class ViewHoder implements Runnable
	{
		private ProductEntity ProductEntity;
		protected ImageView imagePhoto;
		protected ImageView chkSelected;
		protected ImageView btnDelete;
		protected TextView txtPrice;
		protected TextView txtDescription;
		protected TextView txtRemark;
		
		// 定义Handler对象
		private Handler handler  = new Handler()
		{
			public void handleMessage(Message msg)
			{

				{
					if (map.containsKey(ProductEntity.ID))
						imagePhoto.setImageBitmap(map.get(ProductEntity.ID).get());
					else
						imagePhoto.setImageBitmap(null);
				}
			}
		};

		
		public void run()
		{
			if (!map.containsKey(ProductEntity.ID))
			{
				//查找产品的照片数量
				List<ImagesEntity> list = ProductPhotoList.getPhotoInfoList(context, Integer.parseInt(ProductEntity.ID));
				if(list.size()>0)
				{
					for(ImagesEntity image:list)
					{
						if(Utils.Exist(BitmapManager.DIR + "/" + image.getName()))
						{
							map.put(ProductEntity.ID, new SoftReference<Bitmap>(BitmapManager.getResizedImage(BitmapManager.DIR + "/" + image.getName(), width, height)));
							break;
						}

					}
				}

				handler.sendEmptyMessage(0);

			}
		};

		public void show()
		{
			BitmapManager.pool.submit(this);

		}
	}



	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		/*if(OnSelected != null)
		{
			SelectedEntity = (ProductEntity) getItem(arg2);
			OnSelected.onClick(arg0);
		}*/
	}

}
